package com.groupbuying.sysconfig.service.impl;

import com.groupbuying.sysconfig.constants.RedisCacheConstants;
import com.groupbuying.sysconfig.utils.PageUtils;
import com.groupbuying.sysconfig.utils.Query;
import com.groupbuying.sysconfig.dao.SysconfigAreaDao;
import com.groupbuying.sysconfig.domain.SysconfigAreaDO;
import com.groupbuying.sysconfig.dto.AreaDTO;
import com.groupbuying.sysconfig.dto.TreeNode;
import com.groupbuying.sysconfig.service.SysconfigAreaService;
import com.groupbuying.sysconfig.utils.RedisUtils;
import com.groupbuying.sysconfig.utils.TreeBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class SysconfigAreaServiceImpl implements SysconfigAreaService {
    private static final Logger logger = LoggerFactory.getLogger(SysconfigAreaService.class);

    @Autowired
    private SysconfigAreaDao sysconfigAreaDao;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public SysconfigAreaDO get(Integer areaid) {
        return sysconfigAreaDao.get(areaid);
    }

    @Override
    public List<TreeNode> list(Map<String, Object> map) {
        List<SysconfigAreaDO> sysconfigAreaDOList = sysconfigAreaDao.list(map);
        List<TreeNode> treeNodeList = new ArrayList<>();
        for (SysconfigAreaDO sysconfigAreaDO : sysconfigAreaDOList) {
            TreeNode treeNode = new TreeNode();
            treeNode.setId(sysconfigAreaDO.getAreaid().toString());
            treeNode.setParentId(sysconfigAreaDO.getParentid().toString());
            treeNode.setName(sysconfigAreaDO.getAreaname());
            treeNodeList.add(treeNode);
        }
        return TreeBuilder.buildByRecursive(treeNodeList);
    }

    @Override
    public PageUtils areaList(Map<String, Object> map) {
        //按areaId升序排
        map.put("sort", "areaId");
        map.put("order", "asc");

        Query query = new Query(map);
        String redisKey_content = RedisCacheConstants.CACHE_KEY_AREA_CONTENT + query.getPage() + query.getLimit() + query.getOffset();
        String redisKey_count = RedisCacheConstants.CACHE_KEY_AREA_COUNT + query.getPage() + query.getLimit() + query.getOffset();

        //缓存判定
        if(redisUtils.isHasKey(redisKey_content) && redisUtils.isHasKey(redisKey_count)){
            //命中
            //logger.info("命中缓存，key为:" + redisKey_content);
            List<AreaDTO> areaList = new ArrayList<>();
            areaList = (List)redisUtils.lGetAll(redisKey_content);
            String total = (String)redisUtils.getValue(redisKey_count);
            PageUtils pageUtils = new PageUtils(areaList, Integer.parseInt(total));
            return pageUtils;
        } else {
            List<SysconfigAreaDO> sysconfigAreaDOList = sysconfigAreaDao.list(query);
            List areaList = new ArrayList<>();
            for (SysconfigAreaDO sysconfigAreaDO : sysconfigAreaDOList) {
                AreaDTO areaDTO = new AreaDTO();
                areaDTO.setId(sysconfigAreaDO.getAreaid().toString());
                areaDTO.setValue(sysconfigAreaDO.getAreaname());
                areaDTO.setLabel(sysconfigAreaDO.getAreaname());
                areaDTO.setParentId(sysconfigAreaDO.getParentid().toString());
                areaList.add(areaDTO);
            }
            areaList = TreeBuilder.areaBuildByRecursive(areaList);
            int total = this.count(query);
            PageUtils pageUtils = new PageUtils(areaList, total);

            //新建缓存
            redisUtils.setList(redisKey_content,areaList);
            redisUtils.setKey(redisKey_count,String.valueOf(total));
            //logger.info("新建缓存，key为:" + redisKey_content);

            return pageUtils;
        }
    }


    @Override
    public int count(Map<String, Object> map) {
        return sysconfigAreaDao.count(map);
    }

    @Override
    public int save(SysconfigAreaDO sysconfigArea) {
        return sysconfigAreaDao.save(sysconfigArea);
    }

    @Override
    public int update(SysconfigAreaDO sysconfigArea) {
        return sysconfigAreaDao.update(sysconfigArea);
    }

    @Override
    public int remove(Integer areaid) {
        return sysconfigAreaDao.remove(areaid);
    }

    @Override
    public int batchRemove(Integer[] areaids) {
        return sysconfigAreaDao.batchRemove(areaids);
    }
}
